/**
 * 
 */
package gpl.scotlandyard.android.beans.adapters;

import gpl.scotlandyard.android.activities.AbstractActivity;
import gpl.scotlandyard.beans.MarkedNode;
import gpl.scotlandyard.services.MoveTracker;

import java.util.Collections;

import android.graphics.Color;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/** @author norbert */
public class FugitivePositionList extends ArrayAdapter<MoveTracker> implements MoveTracker.Listener {

  private final AbstractActivity context;

  private final MoveTracker tracker;

  /** 
   * @param context
   * @param resource
   * @param tracker
   * @param container */
  public FugitivePositionList(AbstractActivity context, int resource, MoveTracker tracker) {
    super(context, resource, Collections.singletonList(tracker));
    this.context = context;
    this.tracker = tracker;
    tracker.addListener(this);
    notifyDataSetChanged();
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    TextView text = new TextView(context);
    text.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 30);
    text.setGravity(Gravity.CENTER);
    MarkedNode node = tracker.getPossiblePositions().get(position);
    text.setText(node.getNumber() + "");
    if (node.getMark() == MarkedNode.HOPEFULLY_MARK) {
      text.setTextColor(Color.GREEN);
    } else if (node.getMark() == MarkedNode.HOPELESS_MARK) {
      text.setTextColor(Color.GRAY);
    } else {
      text.setTextColor(Color.WHITE);
    }
    return text;
  }

  @Override
  public int getCount() {
    return tracker.getPossiblePositions().size();
  }

  @Override
  public void updated() {
    notifyDataSetChanged();
  }

}
